const app = {
  register: Function,
  trigger: Function,
  forget: Function,
  registerMany: Function,
};
const eventMap = {};

app.register = function (eventName: string, fun: Function) {
  eventMap[eventName] = [];
  eventMap[eventName].push(fun);
};
app.registerMany = function (eventName: string, fun: Function) {
  if (!eventMap[eventName]) {
    eventMap[eventName] = [];
  }
  eventMap[eventName].push(fun);
};
app.trigger = function (eventName, ...params) {
  if (eventMap[eventName]) {
    eventMap[eventName].forEach((item: Function) => {
      try {
        // @ts-ignore
        let fn = eval(item);
        fn.apply(this, params);
      } catch (e) {
        console.error(e);
      }
    });
  }
};
app.forget = function (eventName) {
  if (eventMap[eventName]) {
    delete eventMap[eventName];
  }
};
// @ts-ignore
window.app = app;
export default app;
